<?php

namespace backend\controllers;

use backend\models\Goods;
use Yii;
use backend\models\Check;
use backend\models\CheckSearch;
use backend\models\Message;
use yii\helpers\ArrayHelper;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * BannerController implements the CRUD actions for Banner model.
 */
class CheckController extends BaseController
{

    public function behaviors ()
    {
        return [
            'verbs' => [
                'class'   => VerbFilter::className() ,
                'actions' => [
                    'delete' => [ 'POST' ] ,
                ] ,
            ] ,
        ];
    }

    public function actionPurchaseYes()
    {
        $searchModel = new CheckSearch();
        $dataProvider = $searchModel->searchPurchaseYes( Yii::$app->request->queryParams );

        return $this->render( 'purchaseYes' , [
            'searchModel'  => $searchModel ,
            'dataProvider' => $dataProvider ,
        ] );
    }
    public function actionPurchaseNo()
    {
        $searchModel = new CheckSearch();
        $dataProvider = $searchModel->searchPurchaseNo( Yii::$app->request->queryParams );

        return $this->render( 'purchaseNo' , [
            'searchModel'  => $searchModel ,
            'dataProvider' => $dataProvider ,
        ] );
    }
    public function actionPurchaseNever()
    {
        $searchModel = new CheckSearch();
        $dataProvider = $searchModel->searchPurchaseNever( Yii::$app->request->queryParams );

        return $this->render( 'purchaseNever' , [
            'searchModel'  => $searchModel ,
            'dataProvider' => $dataProvider ,
        ] );
    }

    public function actionAllocateYes()
    {
        $searchModel = new CheckSearch();
        $dataProvider = $searchModel->searchAllocateYes( Yii::$app->request->queryParams );

        return $this->render( 'allocateYes' , [
            'searchModel'  => $searchModel ,
            'dataProvider' => $dataProvider ,
        ] );
    }
    public function actionAllocateNo()
    {
        $searchModel = new CheckSearch();
        $dataProvider = $searchModel->searchAllocateNo( Yii::$app->request->queryParams );

        return $this->render( 'allocateNo' , [
            'searchModel'  => $searchModel ,
            'dataProvider' => $dataProvider ,
        ] );
    }
    public function actionAllocateNever()
    {
        $searchModel = new CheckSearch();
        $dataProvider = $searchModel->searchAllocateNever( Yii::$app->request->queryParams );

        return $this->render( 'allocateNever' , [
            'searchModel'  => $searchModel ,
            'dataProvider' => $dataProvider ,
        ] );
    }

    public function actionScrapYes()
    {
        $searchModel = new CheckSearch();
        $dataProvider = $searchModel->searchScrapYes( Yii::$app->request->queryParams );

        return $this->render( 'scrapYes' , [
            'searchModel'  => $searchModel ,
            'dataProvider' => $dataProvider ,
        ] );
    }
    public function actionScrapNo()
    {
        $searchModel = new CheckSearch();
        $dataProvider = $searchModel->searchScrapNo( Yii::$app->request->queryParams );

        return $this->render( 'scrapNo' , [
            'searchModel'  => $searchModel ,
            'dataProvider' => $dataProvider ,
        ] );
    }
    public function actionScrapNever()
    {
        $searchModel = new CheckSearch();
        $dataProvider = $searchModel->searchScrapNever( Yii::$app->request->queryParams );

        return $this->render( 'scrapNever' , [
            'searchModel'  => $searchModel ,
            'dataProvider' => $dataProvider ,
        ] );
    }


    /**
     * @param $id
     *
     * @return string|\yii\web\Response
     * @throws NotFoundHttpException
     *
     * 采购审核通过
     */
    public function actionPass ( $id )
    {

        //创建事务
        $tr = Yii::$app->db->beginTransaction();
        try {
            Check::updateAll(['status'=> 2],['id'=>$id]);
            $model = $this->findModel( $id );
            if(!empty($model)){
                $goods = new Goods();
                $goods['pid']        = $model['pid'];
                $goods['name']       = $model['name'];
                $goods['model']      = $model['model'];
                $goods['num']        = $model['request_num'];
                $goods['price']      = $model['price'];
                $goods['all_price']  = $model['all_price'];
                $goods['buy_time']   = $model['buy_time'];
                $goods['recipient']  = $model['recipient'];
                $goods['provider']   = $model['provider'];
                $goods['history_user']       = $model['history_user'];
                $goods['history_project']    = $model['history_project'];
                $goods['unit']       = $model['unit'];
                $goods['image']      = $model['image'];
                $goods->save(false);
            }
            $message = new Message();
            $message['content'] = '您的审核采购'.$model->request_num.$model->unit.$model->name.'已通过';
            $message['ctime']   = date('Y-m-d H:i:s',time());
            $message['is_pass']    = 2;
            $message['pid']     = $model->pid;
            $message['cid']     = $id;
            $message->insert(false);
            //提交
            $tr->commit();
        } catch (Exception $e) {
            //回滚
            $tr->rollBack();
            echo  "rollback";
        }
        return $this->success( [ 'purchase-never' ] );

    }

    /**
     * Finds the AdminUser model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     *
     * @param integer $id
     *
     * @return Banner the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel ( $id )
    {
        if ( ( $model = Check::findOne( $id ) ) !== NULL ) {
            return $model;
        } else {
            throw new NotFoundHttpException( 'The requested page does not exist . ' );
        }
    }

    /**
     * @param $id
     *
     * @return string|\yii\web\Response
     * @throws NotFoundHttpException
     *
     *    采购审核不通过
     */
    public function actionVeto ( $id )
    {
        $message = new Message();
        $ret = Check::updateAll(['status'=> 3],['id'=>$id]);
        if($ret){
            $model = $this->findModel( $id );
            $message['content'] = '您的审核采购'.$model->request_num.$model->unit.$model->name.'未通过';
            $message['is_pass']    = 3;
            $message['ctime']   = date('Y-m-d H:i:s',time());
            $message['pid']     = $model->pid;
            $message['cid']     = $id;
            if($message->insert(false)){
                return $this->success( [ 'purchase-never' ] );
            }
        }
    }


    /**
     * @param $id
     *
     * @return string|\yii\web\Response
     * @throws NotFoundHttpException
     *
     *    报废审核不通过
     */
    public function actionScrapVeto ( $id )
    {
        $message = new Message();
        $ret = Check::updateAll(['status'=> 3],['id'=>$id]);
        if($ret){
            $model = $this->findModel( $id );
            $message['content'] = '您的审核报废'.$model->request_num.$model->unit.$model->name.'未通过';
            $message['is_pass']    = 3;
            $message['ctime']   = date('Y-m-d H:i:s',time());
            $message['pid']     = $model->pid;
            $message['cid']     = $id;
            if($message->insert(false)){
                return $this->success( [ 'scrap-never' ] );
            }
        }
    }

    /**
     * @param $id
     *
     * @return string|\yii\web\Response
     * @throws NotFoundHttpException
     *
     *    调配审核不通过
     */
    public function actionAllocateVeto ( $id )
    {
        $message = new Message();
        $ret = Check::updateAll(['status'=> 3],['id'=>$id]);
        if($ret){
            $model = $this->findModel( $id );
            $message['content'] = '您的审核调用'.$model->request_num.$model->unit.$model->name.'未通过';
            $message['is_pass']    = 3;
            $message['ctime']   = date('Y-m-d H:i:s',time());
            $message['pid']     = $model->pid;
            $message['cid']     = $id;
            if($message->insert(false)){
                return $this->success( [ 'allocate-never' ] );
            }
        }
    }

    /**
     * @param $id
     *
     * @return string|\yii\web\Response
     * @throws NotFoundHttpException
     *
     *    报废审核通过
     */

    public function actionScrapPass ( $id, $gid )
    {
            $tr = Yii::$app->db->beginTransaction();
            try {
                Check::updateAll(['status' => 2],['id' => $id]);
                $model = Check::findOne( ['id' => $id] );
                $goods = Goods::findOne( ['id' => $gid] );
                $good['num']       = $goods['num'] - $model['request_num'];
                $good['all_price'] = $goods['all_price'] - $model['all_price'];
                Goods::updateAll($good, ['id' => $gid]);

                $message = new  Message();
                $message['content'] = '您的报废审核'.$model->request_num.$model->unit.$model->name.'已通过';
                $message['is_pass'] = 2;
                $message['ctime']   = date('Y-m-d H:i:s',time());
                $message['pid']     = $model->pid;
                $message['cid']     = $id;
                $message->insert(false);

                $tr->commit();
            } catch (Exception $e) {
            //回滚
            $tr->rollBack();
            echo  "rollback";
         }
            return $this->success( [ 'scrap-never' ] );

    }

    /**
     * @param $id
     *
     * @return string|\yii\web\Response
     * @throws NotFoundHttpException
     *
     *    调配审核通过
     */
    public function actionAllocatePass ($id,$gid,$pid)
    {
        $tr = Yii::$app->db->beginTransaction();  //开启事务
        try {
            Check::updateAll(['status' => 2],['id' => $id]);  //修改审核状态
            $model = $this->findModel( $id );
            $find = Goods::findOne(['id' => $gid]);
//            Goods::updateAll([['num'=>'num+'.$model['request_num'],'all_price'=>'all_price+'.$model['all_price']]], ['pid' => $pid]);
            $goodss['num']       =  $find['num']-$model['request_num'];
            $goodss['all_price'] =  $find['all_price']-$model['all_price'];
            Goods::updateAll($goodss, ['id' => $gid]);


            if(empty($ret)){
                $goodsss = new Goods();
                $goodsss['pid']        = $model['pid'];
                $goodsss['name']       = $model['name'];
                $goodsss['model']      = $model['model'];
                $goodsss['num']        = $model['request_num'];
                $goodsss['price']      = $model['price'];
                $goodsss['all_price']  = $model['all_price'];
                $goodsss['buy_time']   = $model['buy_time'];
                $goodsss['recipient']  = $model['recipient'];
                $goodsss['history_user']     = $model['history_user'];
                $goodsss['history_project']  = $model['history_project'];
                $goodsss['recipient']  = $model['recipient'];
                $goodsss['provider']   = $model['provider'];
                $goodsss['unit']       = $model['unit'];
                $goodsss['image']      = $model['image'];
                $goodsss->save(false);
            }
            $message = new Message();
            $message['content'] = '您的审核调用'.$model->request_num.$model->unit.$model->name.'已通过';
            $message['ctime']   = date('Y-m-d H:i:s',time());
            $message['is_pass']    = 2;
            $message['pid']     = $model->pid;
            $message['cid']     = $id;
            $message->insert(false);
            //提交
            $tr->commit();
        } catch (Exception $e) {
            //回滚
            $tr->rollBack();
            echo  "rollback";
        }
        return $this->success( [ 'allocate-never' ] );
    }

}